package example.utils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FileRead {
    public static Object[][] readCsvFile(String filePath) throws IOException {
        List<Object[]> records = new ArrayList<>();
        String line = "";
        FileInputStream fs = new FileInputStream(filePath);
        InputStreamReader ir = new InputStreamReader(fs, StandardCharsets.UTF_8);
        BufferedReader br = new BufferedReader(ir);

        // 读取文件时，忽略第一行的表头
        br.readLine();
        line = br.readLine();

        // 逐行读取文件数据
        while (line != null) {
            String[] temp = line.split(",");  // 分割CSV行数据
            records.add(temp);
            line = br.readLine();  // 继续读取下一行
        }

        System.out.println(records);
        br.close();
        ir.close();
        fs.close();

        // List<Object[]> 转换为 Object[][]
        Object[][] results = new Object[records.size()][4];
        for (int i = 0; i < records.size(); i++) {
            results[i] = records.get(i);
            System.out.println(Arrays.toString(results[i]));
        }

        return results;
    }
}
